package org.dev.app.utils;

import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @description: 生成编码,格式必须是PF+日期+00001
 * @author: wangshikai
 * @time: 2021/10/21 17:02
 */
public class CodeUtils extends BeanUtils {

    public static String generateCode(String rise, String oldCode) {

        //获取时间格式化
        LocalDateTime nowDateTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String dateStr = dateTimeFormatter.format(nowDateTime);
        //生成配方编号，PF+日期+00001自增
        String newCode = "";
        if (StringUtils.isEmpty(oldCode)) {
            newCode = rise + dateStr + "00001";
        } else {
            Integer oldSubInt = Integer.valueOf(oldCode.substring(10));
            ++oldSubInt;
            String oldSubStr = String.format("%05d", oldSubInt);
            newCode = rise + dateStr + oldSubStr;
        }

        return newCode;
    }

}

